<template>
  <div class="sortEchartCon" style="width: 100%">
    <div
      :ref="id"
      style="width: 2rem; height: 2rem"
      :style="'margin-left:' + marginleft + 'px'"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      myChart: {},
    };
  },
  props: {
    id: {
      type: String,
      default: "",
    },
    score: {
      type: Number,
      default: 0,
    },
    type: {
      type: String,
      default: "",
    },
    summary: {
      type: Object,
    },
    showName: {
      type: String,
      default: "",
    },
    marginleft: {},
  },
  created() {
    this.$nextTick(() => {
      this.action();
    });
  },
  mounted() {
    var _this = this;
    window.addEventListener("resize", function () {
      _this.myChart.resize();
    });
  },
  methods: {
    resizeEc() {
      this.$nextTick(() => {
        this.myChart.resize();
      });
    },
    action() {
        let _this = this
      var colorType, backgroundColor;
      if (this.type == "left") {
        colorType = new _this.$echarts4.graphic.LinearGradient(0, 0, 1, 0, [
          {
            offset: 0.1,
            color: "rgba(255,52,113,1)",
          },
          {
            offset: 1,
            color: "rgba(255,228,88,1)",
          },
        ]);
        backgroundColor = "rgba(255,52,52,1)";
      } else if (this.type == "right") {
        colorType = new _this.$echarts4.graphic.LinearGradient(0, 0, 1, 0, [
          {
            offset: 0.1,
            color: "rgba(196,44,255,1)",
          },
          {
            offset: 1,
            color: "rgba(7,104,248,1)",
          },
        ]);
        backgroundColor = "rgba(7,104,248,1)";
      }
      var score = 1;
      if (this.score == 0 || this.score == "--") {
        score = 0;
        colorType = "rgba(230,230,230,1)";
        backgroundColor = "rgba(230,230,230,1)";
      }

      // 基于准备好的dom，初始化echarts实例
      this.myChart = _this.$echarts4.init(this.$refs[`${this.id}`]);
      var that = this;
      // 指定图表的配置项和数据
      var option;
      option = {
        series: [
          {
            type: "gauge",
            startAngle: 196,
            endAngle: -16,
            min: 0,
            max: 100,
            radius: "98%",
            axisLine: {
              show: true,
              lineStyle: {
                color: [[1, "rgba(230,230,230,1)"]],
                width: score == 0 ? 1 : 3,
              },
            },
            pointer: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            title: {
              show: false,
            },
            detail: {
              show: false,
            },
            data: [
              {
                value: 0,
                name: that.showName,
              },
            ],
            title: {
              triggerEvent: true, // 是否触发事件
              offsetCenter: [0, "30%"], //设置完成率位置
              formatter: that.showName,
              fontSize: 13,
              backgroundColor: backgroundColor,
              color: "#fff",
              height: 19,
              lineHeight: 19,
              borderRadius: 19,
              padding: [1, 10, 1, 10],
            },
          },
          {
            type: "gauge",
            startAngle: score == 0 ? 0 : 196,
            endAngle: score == 0 ? 0 : -16,
            min: 0,
            max: 100,
            radius: "100%",
            itemStyle: {
              color: colorType,
            },
            pointer: {
              show: false,
            },
            axisLine: {
              round: true,
              lineStyle: {
                color: [[(that.score / 100).toFixed(2), colorType]],
                width: 5,
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            detail: {
              backgroundColor: "#fff",
              borderColor: "#999",
              width: "60%",
              lineHeight: 20,
              height: 20,
              borderRadius: 0,
              offsetCenter: [0, "-23%"],
              valueAnimation: true,
              formatter: function (value) {
                var newValue = "";
                if (score == 0) {
                  newValue = "--";
                  return "{value|" + newValue + "}";
                } else {
                  // 加%
                  return "{value|" + value.toFixed(2) + "%}";
                }
              },
              rich: {
                value: {
                  fontSize: 18,
                  fontWeight: "500",
                  color: score == 0 ? "#999999" : "#000000",
                  lineHeight: 22,
                },
              },
            },
            data: [
              {
                value: that.score,
              },
            ],
            roundCap: true, //顶端圆角
            zIndex: 2,
          },
        ],
      };
      if (option && typeof option === "object") {
        this.myChart.setOption(option);
      }
      // this.myChart.getZr().on('click', function(params) {
      //     // params.topTarget && params.topTarget.style && params.topTarget.style.text == '基金评分')
      //     // 这块 可以获取到 具体点击的是 哪一块 x轴 y轴 或者是 标题 啥的

      // })
    },
  },
};
</script>

<style lang="scss"></style>
